package com.xiaobao.coalproperty.module.user.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.xiaobao.coalproperty.other.util.UserHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class WithdrawActivity extends BaseActivity {

    @BindView(R.id.withdraw_user_money)
    TextView tvUserMoney;
    @BindView(R.id.withdraw_ratio)
    TextView tvRadio;
    @BindView(R.id.withdraw_service_money)
    TextView tvServiceMoney;
    @BindView(R.id.withdraw_reality_money)
    TextView tvRealityMoney;
    @BindView(R.id.withdraw_money)
    EditText edtMoney;
    @BindView(R.id.withdraw_pwd)
    EditText edtPwd;


    @Override
    protected int getContentView() {
        return R.layout.activity_withdraw;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("提现");
        showRight(true);
        setRight("提现说明");
        setRightClickListener(rightOnClickListener);

        edtMoney.addTextChangedListener(mTextWatcher);

        if (UserHelperUtils.getInstance().getCenter() != null) {
            tvUserMoney.setText(UserHelperUtils.getInstance().getCenter().getData().getCurrent_money());
            tvRadio.setText(UserHelperUtils.getInstance().getCenter().getData().getWithdraw_ratio());
        }
    }

    private View.OnClickListener rightOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(WithdrawActivity.this, WithdrawNoteActivity.class));
        }
    };

    public void onWithdrawForgetDealClick(View view) {
        startActivity(new Intent(WithdrawActivity.this, ForgetDealPasswordActivity.class));
    }

    public void onWithdrawSubmitClick(View view) {
        submit();
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //文字变化
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //开始输入
        }

        @Override
        public void afterTextChanged(Editable s) {
            //输入结束
            setEditTextValue();
        }
    };

    private void setEditTextValue() {
        try {
            double ratio = Double.parseDouble(tvRadio.getText().toString());
            double money = Double.parseDouble(edtMoney.getText().toString());
            tvRealityMoney.setText(money * ratio + "");
            tvServiceMoney.setText((money - money * ratio) + "");
        } catch (Exception e) {
        }
    }

    private void submit() {
        OkHttpUtils.post().url(UrlConstant.URL_WITHDRAW)
                .params(HttpHelperUtils.getInstance().withdrawParams(edtMoney.getText().toString(), edtPwd.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    showToast(entity.getMsg());
                    if (entity.getCode() == 0) {
                        finish();
                    }
                } catch (Exception e) {
                }
            }
        });
    }
}
